package pers.hmedical.hospital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import pers.hmedical.base.util.MD5;
import pers.hmedical.base.util.R;
import pers.hmedical.hospital.bean.VO.HospitalSetQueryVO;
import pers.hmedical.hospital.domain.HosInfo;
import pers.hmedical.hospital.mapper.HosInfoMapper;
import pers.hmedical.hospital.service.HosInfoService;

import java.util.List;
import java.util.Random;

/**
 * @author wei-xhh
 * @date 2021-06-12
 */
@Service
public class HosInfoServiceImpl extends ServiceImpl<HosInfoMapper, HosInfo> implements HosInfoService {
    @Override
    public R insertHosInfo(HosInfo hosInfo) {
        HosInfo hosInfo1 = new HosInfo();
        hosInfo1.setName(hosInfo.getName());
        boolean save = this.save(hosInfo1);
        return save ? R.ok() : R.error();
    }

    @Override
    public R findAll() {
        List<HosInfo> list = this.list();
        return R.data(list);
    }

    @Override
    public R removeHospSet(Long id) {
        boolean b = this.removeById(id);
        return b ? R.ok() : R.error();
    }

    @Override
    public R findPageHospSet(long current, long limit, HospitalSetQueryVO hospitalSetQueryVO) {
        Page<HosInfo> page = new Page<>(current, limit);
        QueryWrapper<HosInfo> wrapper = new QueryWrapper<>();
        String hoscode = hospitalSetQueryVO.getHoscode();
        String hosname = hospitalSetQueryVO.getHosname();
        if (!StringUtils.isEmpty(hoscode)) {
            wrapper.eq("hoscode", hoscode);
        }
        if (!StringUtils.isEmpty(hosname)) {
            wrapper.eq("hosname", hosname);
        }
        Page<HosInfo> hosInfoPage = this.page(page, wrapper);
        return R.data(hosInfoPage);
    }

    @Override
    public R saveHospitalSet(HosInfo hosInfo) {
        hosInfo.setStatus(1);
        // 签名密钥
        Random random = new Random();
        hosInfo.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        boolean save = this.save(hosInfo);
        return save ? R.ok() : R.error();
    }

    @Override
    public R getHospSet(long id) {
        HosInfo byId = this.getById(id);
        return R.data(byId);
    }

    @Override
    public R updateHospitalSet(HosInfo hosInfo) {
        boolean b = this.updateById(hosInfo);
        return b ? R.ok() : R.error();
    }

    @Override
    public R batchRemove(List<Long> idList) {
        boolean b = this.removeByIds(idList);
        return b ? R.ok() : R.error();
    }
}
